/* rcctwbu1.p - Container Workbench - Delete all records in a chain           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.10.1.9 $                                                               */
/*V8:ConvertMode=NoConvert                                                    */
/* REVISION: 7.5    LAST MODIFIED: 03/21/95           BY: GWM *J049*          */
/* REVISION: 8.6    LAST MODIFIED: 09/20/96           BY: TSI *K005*          */
/* REVISION: 8.6    LAST MODIFIED: 09/29/96           BY: kxn *K003*          */
/* REVISION: 8.6    LAST MODIFIED: 10/14/96           BY: flm *K003*          */
/* REVISION: 8.6    LAST MODIFIED: 12/17/96   BY: *K03K* Vinay Nayak-Sujir    */
/* REVISION: 8.6    LAST MODIFIED: 11/11/97   BY: *K18W* Suresh Nayak         */
/* REVISION: 8.6E   LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane            */
/* REVISION: 8.6E   LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan           */
/* REVISION: 8.6E   LAST MODIFIED: 06/01/98   BY: *K1NF* Niranjan R.          */
/* REVISION: 8.6E   LAST MODIFIED: 06/08/98   BY: *J2MH* Dana Tunstall        */
/* REVISION: 8.6E   LAST MODIFIED: 08/11/98   BY: *J2TP* Poonam Bahl          */
/* REVISION: 8.6E   LAST MODIFIED: 01/12/99   BY: *J37V* Seema Varma          */
/* REVISION: 8.6E   LAST MODIFIED: 02/17/99   BY: *J394* Poonam Bahl          */
/* REVISION: 9.1    LAST MODIFIED: 06/07/99   BY: *K20P* Sanjeev Assudani     */
/* REVISION: 9.1    LAST MODIFIED: 05/28/99   BY: *N004* Patrick Rowan        */
/* REVISION: 9.1    LAST MODIFIED: 12/02/99   BY: *L0M0* Manish K.            */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00   BY: *N0KP* myb                  */
/* REVISION: 9.1    LAST MODIFIED: 09/05/00   BY: *N0R9* Mudit Mehta          */
/* Revision: 1.10.1.7     BY: Jean Miller    DATE: 03/22/01 ECO: *P008*       */
/* Revision: 1.10.1.8     BY: Ellen Borden   DATE: 07/09/01 ECO: *P007*       */
/* $Revision: 1.10.1.9 $           BY: Rajaneesh S.   DATE: 02/21/02 ECO: *L13N*       */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* INPUT PARAMETERS */
define input parameter abs_recid as recid.
define input parameter l_adjust_alloc like mfc_logical no-undo.
define input parameter l_delete       like mfc_logical no-undo.
define input parameter l_delproc      like mfc_logical no-undo.

/* OUTPUT VARIABLES */
define output parameter abnormal_exit as logical no-undo.

/* SHARED VARIABLES */
define new shared variable new_site like sod_site.

/* LOCAL VARIABLES */
define variable par_id     like abs_par_id no-undo.
define variable ship_from  like abs_shipfrom no-undo.
define variable avail_qty  like sod_qty_pick no-undo.
define variable delete_lad like mfc_logical no-undo.
define variable so_db      like si_db no-undo.
define variable err_flag   as   integer no-undo.
define variable tmp_qty    like sod_qty_pick no-undo.

define variable l_abs_pick_qty like sod_qty_pick no-undo.

/* VARIABLE DEFINITIONS FOR gpfile.i */
{gpfilev.i}
{gpfile.i &file_name = """"rcf_ctrl""""}

so_db = global_db.

find abs_mstr where recid(abs_mstr) = abs_recid no-lock.

assign
   par_id = abs_mstr.abs_id
   ship_from = abs_shipfrom.

/*DETERMINE IF CONTAINER AND LINE CHARGES ARE ENABLED*/
{cclc.i}

/* DELETE ALL CHILD RECORDS */
for each abs_mstr where
         abs_mstr.abs_shipfrom = ship_from
     and abs_mstr.abs_par_id = par_id
exclusive-lock:

   if abs_id begins "I"
   then do:
      /*DELETE USER FIELDS and LINE CHARGE RECORDS*/
      if using_container_charges or using_line_charges then do:

      /* DELETE THE LINE CHARGES FROM THE SHIPPER              */
      /* DELETE THE LINE CHARGES CREATED ON THE SALES ORDER    */
      /* FROM THE SHIPPER AND DECREMENT THE TIMES CHARGED ON   */
      /* THE LINE CHARGES THAT ORIGINATED FROM THE SALES ORDER,*/
      /* WHICH WILL NOT BE DELETED                             */
      /* DELETE ANY USER FIELDS THAT MAY HAVE BEEN CREATED */
         {gprunmo.i
            &program = ""rcabsdd.p""
            &module = "ACL"
            &param = """(input recid(abs_mstr))"""}
      end. /*IF USING_CONTAINER_CHARGES....*/

      {gprun.i ""rcdlabsr.p"" "(input recid(abs_mstr))"}
   end.

   if abs_id begins "I"
   then do:

      /* IS CUST. SEQ. SCHEDULES INSTALLED ? */
      if can-find (mfc_ctrl where
                   mfc_field = "enable_sequence_schedules" and
                   mfc_logical)
         and  file_found
      then do:

         /*DELETE SEQUENCE DATA*/
         {gprunmo.i
            &program = ""rcabssdl.p""
            &module = "ASQ"
            &param = """(input recid(abs_mstr),
                         input '' )"""}
      end. /* if can-find */

   end.  /* if abs_id begins "i" */

   if (abs_mstr.abs_par_id begins "I") then
      delete_lad = yes.
   else
      delete_lad = no.

   if l_adjust_alloc then do:
      /* SET GLOBAL_DB USING SOD_SITE */
      new_site = abs_site.
      {gprun.i ""gpalias.p""}

      /* SWITCH TO THE INVENTORY SITE */
      if so_db <> global_db then do:
         {gprun.i ""gpalias2.p"" "(abs_site, output err_flag)"}
         if err_flag <> 0 and err_flag <> 9 then do:
            /* DATABASE # IS NOT CONNECTED */
            {pxmsg.i &MSGNUM=2510 &ERRORLEVEL=4
               &MSGARG1=" ""'"" +
                  getTermLabel(""FOR_REMOTE_INVENTORY"",30) + ""'"" "}
            abnormal_exit = true.
            return.
         end.
      end.

      /* DELETE ALLOCATION */
      {absupack.i "abs_mstr"  3 22 "l_abs_pick_qty"}

      {gprun.i ""soitallb.p""
         "(input abs_order,
           input abs_line,
           input abs_item,
           input abs_site,
           input abs_loc,
           input abs_lot,
           input abs_ref,
           input - abs_qty * decimal(abs__qad03),
           input - l_abs_pick_qty * decimal(abs__qad03),
           input delete_lad,
           input l_delproc,
           output avail_qty,
           output tmp_qty)"}

      /* SWITCH BACK TO THE SALES ORDER DATABASE */
      if so_db <> global_db then do:

         /* UPDATE SALES ORDER DETAIL QTY ALL AND PICK IN */
         /* REMOTE DATABASE                               */
         if abs_id begins "I" and
            can-find(sod_det where
                     sod_nbr  = abs_order
                 and sod_line = integer(abs_line)
                 and sod_part = abs_item )
         then do:

            {gprun.i ""sosopka2.p""
               "(input abs_mstr.abs_order,
                 input integer(abs_mstr.abs_line),
                 input (- l_abs_pick_qty ) * decimal(abs_mstr.abs__qad03),
                 input l_delproc )"}

            find first sod_det where
                       sod_nbr = abs_order
                   and sod_line = integer(abs_line)
                   and sod_part = abs_item
            exclusive-lock no-error.
            if available sod_det and sod_sched then
               sod_pickdate = ?.

         end. /* IF ABS_ID BEGINS "I" */

         {gprun.i ""gpalias3.p"" "(so_db, output err_flag)"}

         if err_flag <> 0 and err_flag <> 9 then do:
            /* DATABASE # IS NOT CONNECTED */
            {pxmsg.i &MSGNUM=2510 &ERRORLEVEL=4
               &MSGARG1=" ""'"" +
                  getTermLabel(""FOR_SALES_ORDERS"",30) + ""'"" "}
            abnormal_exit = true.
            return.
         end.

      end.

      if abs_id begins "I" and
         can-find(sod_det where
                  sod_nbr  = abs_order
              and sod_line = integer(abs_line)
              and sod_part = abs_item )
      then do:

         {gprun.i ""sosopka2.p""
            "(input abs_mstr.abs_order,
              input integer(abs_mstr.abs_line),
              input (- l_abs_pick_qty ) * decimal(abs_mstr.abs__qad03),
              input l_delproc)"}

         find first sod_det where
                    sod_nbr  = abs_order
                and sod_line = integer(abs_line)
                and sod_part = abs_item
         exclusive-lock no-error.

         if available sod_det and sod_sched then
            sod_pickdate = ?.

      end. /* IF ABS_ID BEGINS "I" */

   end. /* IF L_ADJUST_ALLOC */

   /* Delete all records in a chain for Container Workbench */
   {gprun.i ""rcctwbu1.p""
      "(input  recid(abs_mstr),
        input  l_adjust_alloc,
        input  l_delete,
        input  l_delproc,
        output abnormal_exit)"}.

   if l_delete then do:

      /* Delete custom shipper document information associated
       * with the record */
      for first clc_ctrl
         fields(clc_lotlevel)
         no-lock:
      end. /* FOR FIRST clc_ctrl */


      if  available clc_ctrl
      then do :

         if clc_lotlevel = 1
         then do :
            for each lotw_wkfl
               where lotw_mfguser = mfguser
                 and lotw_lotser  = abs_mstr.abs_lotser
                 and lotw_part    = abs_mstr.abs_item
               exclusive-lock:
               delete lotw_wkfl.
            end. /* FOR EACH lotw_wkfl */
         end. /* IF clc_lotlevel */

         if clc_lotlevel = 2
         then do:
            for each lotw_wkfl
               where lotw_mfguser = mfguser
                 and lotw_lotser  = abs_mstr.abs_lotser
               exclusive-lock:
               delete lotw_wkfl.
            end. /* FOR EACH lotw_wkfl */
         end. /* IF clc_lotlevel */

      end. /* IF AVAILABLE clc_ctrl */

      {gprun.i ""sofsde.p"" "(input recid(abs_mstr))"}

      delete abs_mstr.

   end. /*IF L_DELETE */

   else
   if l_adjust_alloc then do:

      assign
         abs_order = ""
         abs_line = "".

      {abspack.i "abs_mstr" 3 22 "0" }

   end. /*IF L_ADJUST_ALLOC */

end. /* FOR EACH ABS_MSTR .. */
